package com.sx.pipeline.ui.mine

import android.app.Application
import androidx.databinding.ObservableField
import com.blankj.utilcode.util.ToastUtils
import com.sx.mvvmlibs.base.BaseViewModel
import com.sx.mvvmlibs.event.SingleLiveEvent
import com.sx.mvvmlibs.http.CommonObserver
import com.sx.mvvmlibs.utils.Logs
import com.sx.mvvmlibs.utils.RxThreadHelper
import com.sx.pipeline.app.model.CYBaseViewModel
import com.sx.pipeline.repos.http.res.IdentityVO

/**
 * @Author: YY
 * @Date: 2022/6/27
 * @Desc: TODO
 */
class IdentityManageVM(app: Application): CYBaseViewModel(app) {

    /**
     * 获取当前可申请的单位列表
     */
    val identityListEvent: SingleLiveEvent<List<IdentityVO>> = SingleLiveEvent()

    fun getUnitIdentity() {
        addSubscribe(mModel.getUnitIdentity()
            .compose(RxThreadHelper.rxSchedulerHelper())
            .subscribeWith(object : CommonObserver<List<IdentityVO>, BaseViewModel<*>>
                (this, true) {
                override fun onResult(result: List<IdentityVO>) {
                    Logs.d("getUnitIdentity ${result.size}")
                    identityListEvent.postValue(result)
                }
            })
        )
    }

    val identityField: ObservableField<String> = ObservableField();

    fun applyIdentity() {
        addSubscribe(mModel.applyIdentity(identityField.get().toString())
            .compose(RxThreadHelper.rxSchedulerHelper())
            .subscribeWith(object : CommonObserver<Any, BaseViewModel<*>>
                (this, true) {
                override fun onResult(result: Any) {
                    ToastUtils.showLong(result as String)
                    finish()
                }
            }))
    }
}